﻿#include "housemanageedit.h"
#include "ui_housemanageedit.h"
#include <QFileDialog>
#include <QMessageBox>

HouseManageEdit::HouseManageEdit(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HouseManageEdit)
{
    ui->setupUi(this);

    m_state = E_None;
    m_haveImage = false;
}

HouseManageEdit::~HouseManageEdit()
{
    delete ui;
}

void HouseManageEdit::setIndex(int index)
{
    m_index = index;
}

void HouseManageEdit::setHotelID(const QString& hotelID)
{
    m_house.setHotel(hotelID);
}

void HouseManageEdit::setState(EditState state)
{
    m_state = state;
}

/////////////////////////////////////////////watch Details////////////////////////////////////////////////
void HouseManageEdit::setHouseInfosShow(const HouseInfo& house)
{
    m_house = house;
    m_haveImage = false;
    m_imagePathList.clear();

    ui->pb_submit->setText("修改");
    ui->pb_cancel->setText("退出");
    ui->lb_areaReminder->close();
    ui->lb_typeReminder->close();

    ui->cb_net->setCurrentText(m_house.getNet());
    ui->cb_media->setCurrentText(m_house.getMedia());
    ui->cb_window->setCurrentText(m_house.getWindow());
    ui->cb_bathroom->setCurrentText(m_house.getBathroom());
    ui->cb_breakfast->setCurrentText(m_house.getBreakfast());
    ui->cb_capacity->setCurrentText(QString::number(m_house.getCapacity()));

    ui->le_area->setText(m_house.getArea());
    ui->le_houseType->setText(m_house.getType());
    ui->sb_price->setValue(m_house.getPrice());
    ui->sb_houseCount->setValue(m_house.getCount());

    if (ui->cb_bedType->findText(m_house.getBedType()) == -1)
    {
        ui->cb_bedType->addItem(m_house.getBedType());
    }
    ui->cb_bedType->setCurrentText(m_house.getBedType());

    m_bathroomAcc = m_house.getBathroomAcc().split(",");
    if (m_bathroomAcc.contains(ui->cb_hotWater->text()))
    {
        ui->cb_hotWater->setChecked(true);
        m_bathroomAcc.removeOne(ui->cb_hotWater->text());
    }
    if (m_bathroomAcc.contains(ui->cb_bath->text()))
    {
        m_bathroomAcc.removeOne(ui->cb_bath->text());
        ui->cb_bath->setChecked(true);
    }
    if (m_bathroomAcc.contains(ui->cb_bathThings->text()))
    {
        m_bathroomAcc.removeOne(ui->cb_bathThings->text());
        ui->cb_bathThings->setChecked(true);
    }
    if (m_bathroomAcc.contains(ui->cb_hearDrier->text()))
    {
        m_bathroomAcc.removeOne(ui->cb_hearDrier->text());
        ui->cb_hearDrier->setChecked(true);
    }

    forbidEveryOne();
}
void HouseManageEdit::setHouseImageShow(int index, const ImageList& imageList)
{
    if (index < 0)
    {
        return;
    }

    switch (index)
    {
        default:
        case 0:
        {
            ui->lb_image1->close();
            ui->lb_image2->close();
            ui->lb_image3->close();
            ui->lb_image4->close();
            ui->lb_image5->close();
        } break;
        case 1:
        {
            ui->lb_image1->show();
            ui->lb_image2->close();
            ui->lb_image3->close();
            ui->lb_image4->close();
            ui->lb_image5->close();
            ui->lb_image1->setPixmap(QPixmap::fromImage(imageList.at(0).scaled(100,100)));
        } break;
        case 2:
        {
            ui->lb_image1->show();
            ui->lb_image2->show();
            ui->lb_image3->close();
            ui->lb_image4->close();
            ui->lb_image5->close();
            ui->lb_image1->setPixmap(QPixmap::fromImage(imageList.at(0).scaled(100,100)));
            ui->lb_image2->setPixmap(QPixmap::fromImage(imageList.at(1).scaled(100,100)));
        } break;
        case 3:
        {
            ui->lb_image1->show();
            ui->lb_image2->show();
            ui->lb_image3->show();
            ui->lb_image4->close();
            ui->lb_image5->close();
            ui->lb_image1->setPixmap(QPixmap::fromImage(imageList.at(0).scaled(100,100)));
            ui->lb_image2->setPixmap(QPixmap::fromImage(imageList.at(1).scaled(100,100)));
            ui->lb_image3->setPixmap(QPixmap::fromImage(imageList.at(2).scaled(100,100)));
        } break;
        case 4:
        {
            ui->lb_image1->show();
            ui->lb_image2->show();
            ui->lb_image3->show();
            ui->lb_image4->show();
            ui->lb_image5->close();
            ui->lb_image1->setPixmap(QPixmap::fromImage(imageList.at(0).scaled(100,100)));
            ui->lb_image2->setPixmap(QPixmap::fromImage(imageList.at(1).scaled(100,100)));
            ui->lb_image3->setPixmap(QPixmap::fromImage(imageList.at(2).scaled(100,100)));
            ui->lb_image4->setPixmap(QPixmap::fromImage(imageList.at(3).scaled(100,100)));
        } break;
        case 5:
        {
            ui->lb_image1->show();
            ui->lb_image2->show();
            ui->lb_image3->show();
            ui->lb_image4->show();
            ui->lb_image5->show();
            ui->lb_image1->setPixmap(QPixmap::fromImage(imageList.at(0).scaled(100,100)));
            ui->lb_image2->setPixmap(QPixmap::fromImage(imageList.at(1).scaled(100,100)));
            ui->lb_image3->setPixmap(QPixmap::fromImage(imageList.at(2).scaled(100,100)));
            ui->lb_image4->setPixmap(QPixmap::fromImage(imageList.at(3).scaled(100,100)));
            ui->lb_image5->setPixmap(QPixmap::fromImage(imageList.at(4).scaled(100,100)));
        } break;
    }
}
void HouseManageEdit::forbidEveryOne()
{
    ui->cb_net->setEnabled(false);
    ui->cb_breakfast->setEnabled(false);
    ui->cb_media->setEnabled(false);
    ui->le_houseType->setEnabled(false);
    ui->sb_price->setEnabled(false);
    ui->sb_houseCount->setEnabled(false);
    ui->cb_bedType->setEnabled(false);
    ui->cb_window->setEnabled(false);
    ui->cb_capacity->setEnabled(false);
    ui->le_area->setEnabled(false);
    ui->cb_bathroom->setEnabled(false);

    ui->cb_bath->setEnabled(false);
    ui->cb_bathThings->setEnabled(false);
    ui->cb_hearDrier->setEnabled(false);
    ui->cb_hotWater->setEnabled(false);
    ui->pb_uploadPic->setEnabled(false);
}
void HouseManageEdit::allowEveryOne()
{
    ui->cb_net->setEnabled(true);
    ui->cb_breakfast->setEnabled(true);
    ui->cb_media->setEnabled(true);
    ui->le_houseType->setEnabled(true);
    ui->sb_price->setEnabled(true);
    ui->sb_houseCount->setEnabled(true);
    ui->cb_bedType->setEnabled(true);
    ui->cb_window->setEnabled(true);
    ui->cb_capacity->setEnabled(true);
    ui->le_area->setEnabled(true);
    ui->cb_bathroom->setEnabled(true);

    ui->cb_bath->setEnabled(true);
    ui->cb_bathThings->setEnabled(true);
    ui->cb_hearDrier->setEnabled(true);
    ui->cb_hotWater->setEnabled(true);
    ui->pb_uploadPic->setEnabled(true);
}

/////////////////////////////////////////////create new house/////////////////////////////////////////////
void HouseManageEdit::on_pb_uploadPic_clicked()
{
    m_imagePathList = QFileDialog::getOpenFileNames(this, tr("Open Image"),
                                                            "F:\\ForWork\\HotelPlatform\\image\\head_pic\\avatar",
                                                            tr("Image (*.png *.jpg)"));

    if (m_imagePathList.size() == 0)
    {
        return;
    }

    ui->lb_image1->show();
    ui->lb_image2->show();
    ui->lb_image3->show();
    ui->lb_image4->show();
    ui->lb_image5->show();

    ui->lb_image1->clear();
    ui->lb_image2->clear();
    ui->lb_image3->clear();
    ui->lb_image4->clear();
    ui->lb_image5->clear();
    switch (m_imagePathList.size())
    {
        case 5:
            ui->lb_image5->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(4)).scaled(100,100)));
        case 4:
            ui->lb_image4->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(3)).scaled(100,100)));
        case 3:
            ui->lb_image3->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(2)).scaled(100,100)));
        case 2:
            ui->lb_image2->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(1)).scaled(100,100)));
        case 1:
        {
            ui->lb_image1->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(0)).scaled(100,100)));
            m_haveImage = true;
        } break;
        default:
        {
            ui->lb_image1->setText("至多上传5张图片");
            m_haveImage = false;
        } break;
    }
}
void HouseManageEdit::on_le_houseType_editingFinished()
{
    QString arg1 = ui->le_houseType->text();
    if (m_state == E_NewCreate || m_state == E_EditInfo)
    {
        if (arg1.isEmpty())
        {
            ui->le_houseType->setPlaceholderText("请填写酒店房间类型名称");
        }
        else
        {
            QRegExp password_reg_exp("^[\u4e00-\u9fa5]+$");///
            QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
            if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
            {
                ui->le_houseType->clear();
                ui->le_houseType->setPlaceholderText("格式错误,重新输入");
            }
        }
    }
}
void HouseManageEdit::on_le_houseType_textChanged(const QString &arg1)
{
    if (m_state == E_NewCreate || m_state == E_EditInfo)
    {
        if (arg1.isEmpty())
        {
            ui->le_houseType->setPlaceholderText("请填写酒店房间类型名称");
            ui->le_houseType->setText("");
            ui->lb_typeReminder->setText("");
        }
        else
        {
            QRegExp password_reg_exp("^[\u4e00-\u9fa5]+$");///
            QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
            if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
            {
                 ui->lb_typeReminder->setText("X");
            }
            else
            {
                 ui->lb_typeReminder->setText(" ");
            }
        }
    }
}
void HouseManageEdit::on_le_area_editingFinished()
{
    if (m_state == E_NewCreate || m_state == E_EditInfo)
    {
        QString arg1 = ui->le_area->text();
        if (arg1.isEmpty())
        {
            ui->le_area->setPlaceholderText("面积(㎡)");
            ui->le_area->setText("");
        }
        else
        {
            QRegExp password_reg_exp("^[0-9]{0,3}|[0-9]{0,3}-[0-9]{0,3}|[0-9]{0,3}-[0-9]{0,3}㎡|[0-9]{0,3}㎡$");///
            QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
            if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
            {
                ui->le_area->clear();
                ui->le_area->setPlaceholderText("格式错误,重新输入");
            }
        }
    }
}
void HouseManageEdit::on_le_area_textChanged(const QString &arg1)
{
    if (m_state == E_NewCreate || m_state == E_EditInfo)
    {
        if (arg1.isEmpty())
        {
            ui->le_area->setPlaceholderText("面积(㎡)");
            ui->le_area->setText("");
            ui->lb_areaReminder->setText("");
        }
        else
        {
            QRegExp reg_exp("^[0-9]{0,3}|[0-9]{0,3}-[0-9]{0,3}$");///
            QRegExpValidator *validator = new QRegExpValidator(reg_exp);
            if(!validator->regExp().exactMatch(arg1))/// 匹配不对
            {
                 ui->lb_areaReminder->setText("X");
            }
            else
            {
                 ui->lb_areaReminder->setText(" ");
            }
        }
    }
}

//////////////////////////////////////////////// common /////////////////////////////////////////////////
void HouseManageEdit::on_pb_submit_clicked()
{
    QString text = ui->pb_submit->text();
    if (text == "修改")
    {
        m_state = E_EditInfo;
        ui->pb_submit->setText("提交");
        ui->pb_cancel->setText("取消");
        allowEveryOne();
    }
    else                    //// 提交
    {
        int price = ui->sb_price->value();
        int count = ui->sb_houseCount->value();
        QString houseType = ui->le_houseType->text();
        QString area = ui->le_area->text();

        if (price > 0 && count > 0  && (!houseType.isEmpty()) && (!area.isEmpty()))
        {
            if (!area.contains("㎡"))
            {
                area += "㎡";
            }
            int capacity = ui->cb_capacity->currentText().toInt();
            QString window = ui->cb_window->currentText();
            QString bedType = ui->cb_bedType->currentText();
            QString bathroom = ui->cb_bathroom->currentText();
            QString net = ui->cb_net->currentText();
            QString breakfast = ui->cb_breakfast->currentText();
            QString media = ui->cb_media->currentText();
            QString bathroomAcc = m_bathroomAcc.join(",");

            if (m_state == E_NewCreate)
            {
                QString hotelID = m_house.getHotel();
                QString houseID = hotelID + "_" + QString::number(m_index);
                HouseInfo house(houseID, hotelID,houseType,price, count, area, capacity,bedType ,bathroom,
                               window, net, breakfast, media,bathroomAcc);

                if (m_imagePathList.size() != 0)
                {
                    house.setPhoto(m_imagePathList.join("#"));
                }
                else
                {
                    house.setPhoto("");
                }
                GlobalVars::g_localHouse = house;
                emit signalSubmitHouseInfo();
            }
            else if (m_state == E_EditInfo)
            {
                if (m_house.getType() == houseType)
                {
                    houseType = "";
                }
                if (m_house.getPrice() == price)
                {
                    price = 0;
                }
                if (m_house.getCount() == count)
                {
                    count = 0;
                }
                if (m_house.getArea() == area)
                {
                    area = "";
                }
                if (m_house.getCapacity() == capacity)
                {
                    capacity = 0;
                }
                if (m_house.getBedType() == bedType)
                {
                    bedType = "";
                }
                if (m_house.getBathroom() == bathroom)
                {
                    bathroom = "";
                }
                if (m_house.getWindow() == window)
                {
                    window = "";
                }
                if (m_house.getNet() == net)
                {
                    net = "";
                }
                if (m_house.getBreakfast() == breakfast)
                {
                    breakfast = "";
                }
                if (m_house.getMedia() == media)
                {
                    media = "";
                }
                if (m_house.getBathroomAcc() == bathroomAcc)
                {
                    bathroomAcc = "";
                }

                QString hotelID = m_house.getHotel();
                QString houseID = m_house.getHouse();
                HouseInfo house(houseID, hotelID,houseType,price, count, area, capacity,bedType ,bathroom,
                               window, net, breakfast, media,bathroomAcc);

                if (m_imagePathList.size() != 0)
                {
                    house.setPhoto(m_imagePathList.join("#"));
                }
                else
                {
                    house.setPhoto("");
                }
                GlobalVars::g_localHouse = house;
                emit signalUpdateHouseInfo();
            }
            else
            {
                return;
            }
        }
        else
        {
            QMessageBox::warning(this, "警告", "请完善信息", QMessageBox::Yes, QMessageBox::Yes);
        }
    }
}

void HouseManageEdit::constructeCondition(bool checked, const QString& text)
{
    if (checked)
    {
        m_bathroomAcc.append(text);
    }
    else
    {
        if (m_bathroomAcc.contains(text))
        {
            m_bathroomAcc.removeOne(text);
        }
    }
}
void HouseManageEdit::on_cb_hotWater_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_hotWater->text());
}
void HouseManageEdit::on_cb_bath_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_bath->text());
}
void HouseManageEdit::on_cb_bathThings_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_bathThings->text());
}
void HouseManageEdit::on_cb_hearDrier_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_hearDrier->text());
}

void HouseManageEdit::on_cb_bedType_currentIndexChanged(const QString &arg1)
{
    if (arg1 == "自定义")
    {
        m_edit = new QLineEdit(this);
        m_edit->setFont(QFont("微软雅黑", 11));
        ui->cb_bedType->setLineEdit(m_edit);
        ui->cb_bedType->lineEdit()->clear();
        ui->cb_bedType->lineEdit()->setFocus();
    }
    else
    {
        ui->cb_bedType->setEditable(false);
    }
}

void HouseManageEdit::on_pb_cancel_clicked()
{
    QString text = ui->pb_cancel->text();
    if (text == "退出")
    {
        this->close();
    }
    else
    {
        ui->pb_cancel->setText("退出");
        ui->pb_submit->setText("修改");
        forbidEveryOne();
    }
}
